<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;
/**
 * 用户发送短信 模型
 * @package app\user_center\model
 */
class UcMemberMobileSendcode extends Model
{
     // 设置当前模型对应的完整数据表名称
     protected $table = 'xm_uc_member_mobile_sendcode';

     //短信类型 短信发送类型 0注册/登录 1找回密码 2更换手机（旧） 3绑定手机 4更换手机（新） 5头衔认证
     public function getMobileTypeAttr($value){
          $status = [0=>'注册/登录', 1=>'找回密码', 2=>'更换手机（旧）', 3=>'绑定手机', 4 => '更换手机（新）', 5 => '头衔认证'];
          return $status[$value];
     }

     //短信状态
     public function getMobileStatusAttr($value){
          $status = [0=>'未使用', 1=>'已使用', 2=>'token待使用'];
          return $status[$value];
     }

	/**
     * 查询有效短信
     * @param int $mobile     用户手机号码
     * @param int $mobile_type       短信类型
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getDataByMobile($mobile, $mobile_type, $mobile_status = 0){
		$map = [];
          $map['mobile'] 			= $mobile;
          $map['mobile_type'] 	     = $mobile_type;
          $map['mobile_status'] 	     = $mobile_status;
          $result = $this -> where($map)->whereTime('expire_time', '>', time())->order('create_time', 'desc')->find();
          if($result){
               return array_out(200, 'success', $result);
          }else{
               return array_out(-1002, 'error33');
          }
	}
	/**
     * 查询有效token
     * @param int $mobile     用户手机号码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getDataByMobileToken($mobile_token){
		$map = [];
          $map['mobile_token'] 	= $mobile_token;
          $map['mobile_status'] 	= 2;
          $info = self::where($map)->order('id', 'desc')->find();
          if(!empty($info)){
               return array_out(200, 'success', $info);
           }else{
               return array_out(-1001, 'error');
           }
	}
	
	/**
     * 查询今日发送短信条数
     * @param int $mobile     用户手机号码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getDataTodayTimes($mobile){
          $map = [];
          $map['mobile'] = $mobile;
          $num =  self::field('id')->whereTime('create_time', 'today')->where($map)->count('id');
          return array_out(200, 'success', $num);
    }
	
	/**
     * 插入发送记录
     * @param int $mobile     用户手机号码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function addData($mobile, $code, $mobile_type){
		$send_code_expire = config('send_code_expire');//10分钟有效
		
		$this->mobile 		= $mobile;
		$this->code 		= $code;
		$this->mobile_type 	= $mobile_type;
		$this->expire_in 	= $send_code_expire;
		$this->expire_time 	= time() + $send_code_expire;
          $res = $this -> isUpdate(false) -> save();//添加  thinkphp主键bug
          if($res){
               return array_out(200, 'success');
           }else{
               return array_out(-1001, 'error');
           }
	}
	/**
     * 验证码更新使用状态
     * @param int $send_id     当前验证码ID
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function useData($send_id){
		$map = [];
		$map['mobile_status']	= 1;
          $res = self::where('id' ,'=' , $send_id) ->update($map);
          if($res){
               return array_out(200, 'success');
           }else{
               return array_out(-1001, 'error');
           }
     }
     
	public function useDataToken($mobile_token){
		$map = [];
		$map['mobile_status']	= 1;
          $res = self::where('mobile_token' ,'=' , $mobile_token) ->update($map);
          if($res){
               return array_out(200, 'success');
           }else{
               return array_out(-1001, 'error');
           }
	}
	
	/**
     * 验证码插入验证token
     * @param int $send_id     当前验证码ID
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function addDataToken($send_id, $mobile_token){
		$map = [];
		$map['mobile_token']	= $mobile_token;
		$map['mobile_status']	= 2;
          $res = self::where('id' ,'=' , $send_id) ->update($map);
          if($res){
               return array_out(200, 'success');
           }else{
               return array_out(-1001, 'error');
           }
	}
}
